package com.xbjtech.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xbjtech.model.User;
import com.xbjtech.service.UserService;
@Controller
public class UserController {
	
	@Autowired
	UserService userService;
	
	
	
	
	/**
	 * 新增
	 */
	@PostMapping("/add")
	public String insert(User user) {
		userService.insertUser(user);
		return "redirect:/users";
	}
	
	/**
	 * 列表
	 * @return
	 */
	@GetMapping("/users")
	public String list(Model model) {
		List<User> list = userService.list();
		model.addAttribute("list", list);
		return "list";
	}
	
	@PostMapping("/user")
	public String update(User user) {
		userService.updateUser(user);
		return "redirect:/users";
	}

	
	@GetMapping("/user/{id}")
	public String delete(@PathVariable Integer id) {
		userService.deleteUserById(id);
		return "redirect:/users";
	}
	
	@GetMapping("/user/add")
	public String addPage(Model model) {
		User user = new User();
		model.addAttribute("user", user);
		return "add";
	}
	
	@GetMapping("/user/edit/{id}")
	public String editPage(@PathVariable Integer id, Model model) {
		User user = userService.selectUserById(id);
		model.addAttribute("user", user);
		return "update";
	}

}
